<?php

include_once 'Answer_Patterns.php';
include_once 'Mount_DOM_tree.php';

class Refactoring_Table_Layout_Div extends Answer_Patterns {

   public function start($domDocument) {
      $result = "";
      $mount_tree = new Mount_DOM_tree();

      $search = $domDocument->getElementsByTagName('table');
      //parentNode pai

      $scriptCSS = "&LT;style&GT;
*{ margin: 0; padding: 0; } <br/>
#todos{
    width: 950px; /* Largura */
    background: black; /* Cor de fundo */
    margin:0 auto; /* margem para topo e abaixo igual a 0, automático para as laterais */
}<br/>
header{
    height: 150px; /* Altura */
    background: green; /* Cor de fundo */
    margin-bottom: 10px; /* Empurra os elementos para baixo */
}<br/>
footer{
    height: 50px;
    background: green;
    clear: both;
}<br/>
#menu{
    float: left;
    width: 200px;
    height: 400px;
    background: appworkspace;
    margin-bottom: 10px;
}<br/>
#conteudo{
    float: right;
    width: 740px;
    height: 400px;
    background: appworkspace;
}<br/>
&LT;/style&GT;";

      $html = '&LT;body&GT;<br/>&nbsp&nbsp&nbsp&LT;div id="todos"&GT;<br/>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
    &LT;header&GT;...&LT;/header&GT;<br/>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
    &LT;nav id="menu"&GT;...&LT;/nav&GT;<br/>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
    &LT;content id="conteudo"&GT;...&LT;/content&GT;<br/>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
    &LT;footer&GT;...&LT;/footer&GT;<br/>&nbsp&nbsp&nbsp
&LT;/div&GT;<br/>
    &LT;/body&GT;';
      
      $scriptCSS_menu_dir = "&LT;style&GT;
*{ margin: 0; padding: 0; } <br/>
#todos{
    width: 950px; /* Largura */
    background: black; /* Cor de fundo */
    margin:0 auto; /* margem para topo e abaixo igual a 0, automático para as laterais */
}<br/>
header{
    height: 150px; /* Altura */
    background: green; /* Cor de fundo */
    margin-bottom: 10px; /* Empurra os elementos para baixo */
}<br/>
footer{
    height: 50px;
    background: green;
    clear: both;
}<br/>
#menu{
    float: right;
    width: 200px;
    height: 400px;
    background: appworkspace;
    margin-bottom: 10px;
}<br/>
#conteudo{
    float: left;
    width: 740px;
    height: 400px;
    background: appworkspace;
}<br/>
&LT;/style&GT;";

      $html_menu_dir = '&LT;body&GT;<br/>&nbsp&nbsp&nbsp&LT;div id="todos"&GT;<br/>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
    &LT;header&GT;...&LT;/header&GT;<br/>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
    &LT;content id="conteudo"&GT;...&LT;/content&GT;<br/>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
    &LT;nav id="menu"&GT;...&LT;/nav&GT;<br/>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
    &LT;footer&GT;...&LT;/footer&GT;<br/>&nbsp&nbsp&nbsp
&LT;/div&GT;<br/>
    &LT;/body&GT;';

      foreach ($search as $f) {
         $trs = $f->getElementsByTagName('tr');
         if ($trs->length == 3) {
            if ($trs->item(1)->getElementsByTagName('td')->length == 2) {
               if (!empty($trs->item(1)->getElementsByTagName('td')->item(0)->getAttribute('width')) && $trs->item(1)->getElementsByTagName('td')->item(0)->getAttribute('width') > 0) {                  
                  $result .= parent::elementAnswerPatterns("&LT;$f->nodeName&GT;", $f->getLineNo(), $answer, "Substitua por elemetos e CSS", " e parece ser uma estrutura de layout da página");
                  $result .= parent::tableAnswer($mount_tree->structHTML($f, 2, 0), $scriptCSS . "<br/>" . $html);
               } else if (!empty($trs->item(1)->getElementsByTagName('td')->item(1)->getAttribute('width'))) {
                  $result .= parent::elementAnswerPatterns("&LT;$f->nodeName&GT;", $f->getLineNo(), $answer, "Substitua por elemetos e CSS", " e parece ser uma estrutura de layout da página");
                  $result .= parent::tableAnswer($mount_tree->structHTML($f, 2, 0), $scriptCSS_menu_dir . "<br/>" . $html_menu_dir);
               }
            } else if ($trs->item(1)->getElementsByTagName('td')->length == 3) {
               if (!empty($trs->item(1)->getElementsByTagName('td')->item(0)->getAttribute('width')) &&
                       !empty($trs->item(1)->getElementsByTagName('td')->item(2)->getAttribute('width'))) {

               }
            }
         }
      }
      if (!empty($result)) {
         $result = parent::titleAnswerPatterns("Substituição de layouts baseados em tabelas") . $result;
      }
      return $result;
   }

}
?>